package 我的Java学习_算法基础.day_04;

public class _060_顺时针打印二维数组 {

    public static void main(String[] args) {
        int[][]a ={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
        print(a);
    }
    /**
     * 1	2	3	4
     *
     * 5	6	7	8
     *
     * 9	10	11	12
     *
     * 13	14	15	16
     *
     * 输出：1，2，3，4，8，12，16，15，14，13，9，5，6，7，11，10
     * @param array
     */
    private static void print(int[][] array){
            int leftUpRow = 0,leftUpCol =0,rightDownRow = array.length-1,rightDownCol = array[0].length-1;
            while(leftUpRow<=rightDownRow&&leftUpCol<=rightDownCol) {
                int r = leftUpRow, c = leftUpCol;
                //上面一条边
                while (c <= rightDownCol) {
                    System.out.print(array[r][c++] + " ");
                }
                //恢复
                c = rightDownCol;
                r++;
                //右边一条边
                while (r <= rightDownRow) {
                    System.out.print(array[r++][c] + " ");
                }
                //恢复
                r = rightDownRow;
                c--;
                //下面一条边
                while (c >= leftUpCol) {
                    System.out.print(array[r][c--] + " ");
                }
                //恢复
                c = leftUpCol;
                r--;
                while (r > leftUpRow) {
                    System.out.print(array[r--][c] + " ");
                }
                leftUpCol++;
                leftUpRow++;
                rightDownCol--;
                rightDownRow--;
            }

    }
}
